const myAxios = function (confilg) {
  return new Promise((resolve, reject) => {
    // console.log(filg)
    const { url, method, data, params } = confilg
    const xhr = new XMLHttpRequest()
    if (method.toUpperCase() === 'GET') {
      const str = setparams(params)
      xhr.open('GET', params ? url + '?' + str : url)
      xhr.send()
    } else {
      xhr.open('POST', url)
      let flag = Object.prototype.toString.call(data) === '[object object]'

      xhr.setRequestHeader(
        'Content-Type',
        flag ? 'application/json' : 'application/x-www-form-urlencoded'
      )
      xhr.send(flag ? JSON.stringify(data) : data)
    }

    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if ((xhr.status >= 200) & (xhr.status < 300)) {
          resolve(JSON.parse(xhr.responseText))
        } else {
          reject('失败请求码' + xhr.status)
        }
      }
    }
  })
}
function setparams(obj) {
  const arr = []
  for (let key in obj) {
    arr.push(key + '=' + obj[key])
  }
  return arr.join('&')
}
